package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.TransferClass;

@Repository
public interface TransferClassDao extends JpaRepository<TransferClass, Integer> {

    @Transactional
    @Modifying
    Integer deleteByTransferClassId(Integer transferClassId);

    List<TransferClass> findByTransferClassId(Integer transferClassId);

    @Transactional
    @Modifying
    Integer deleteByTransferClassIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id")
    List<Map<String, Object>> queryAllTransferClassAndUsersInfo();

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "where tc.originalClass LIKE CONCAT('%',:originalClass,'%')")
    List<Map<String, Object>> findByOriginalClassLike(@Param("originalClass") String originalClass);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "where tc.transferStatus LIKE CONCAT('%',:transferStatus,'%')")
    List<Map<String, Object>> findByTransferStatusLike(@Param("transferStatus") String transferStatus);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "where tc.transferReason LIKE CONCAT('%',:transferReason,'%')")
    List<Map<String, Object>> findByTransferReasonLike(@Param("transferReason") String transferReason);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,tc.* \r\n" +
            "from users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id AS userId,u.account AS userName,u.nickName,tc.*\r\n" +
            "FROM users u INNER JOIN transfer_class tc ON u.user_Id = tc.user_Id\r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectTransferClassByUserId(@Param("userId") Integer userId);

}
